package com.net.jef.webx.fen.tool.module.screen;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.fastjson.JSONObject;
import com.net.jef.util.HttpClientHelper;

public class IpService {
    // http://www.youdao.com/smartresult-xml/search.s?type=ip&q=42.120.16.111
    //http://whois.www.net.cn/whois/api_whois?host=sina.com.cn
    
//    获取Ip所在地：
//    http://www.youdao.com/smartresult-xml/search.s?type=ip&q=58.30.32.61<!---->
//    <?xml version="1.0" encoding="gbk"?>
//    <smartresult>
//    <product type="ip">
//    <ip>61.172.201.194</ip>
//    <location>上海市 电信张江机房</location>
//    </product>
//    </smartresult>

    //
    
    
//    身份证信息：
//    http://www.youdao.com/smartresult-xml/search.s?type=id&q=232700198910206016<!---->
//
//    获取手机所在地：
//    http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=13671151172<!---->
    
    private static Pattern      ipPattern = null;
    private static Pattern      servicePattern = null;
    
    static {
        try {
            ipPattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");

        } catch (Exception e) {
        }
    }
    public void execute(@Param("code") String code, Context context,
            Navigator nav,HttpServletRequest request) throws Exception {
        String ip= null;
        String domain=null;
        
        if(code==null) {
            code =request.getRemoteAddr();
        }else{
            code=code.trim();
        }
        
        Matcher matcher = ipPattern.matcher(code);
        if(matcher.matches()){
            ip= code;
        }else{
            try {
            InetAddress ad = InetAddress.getByName(code);
            ip=ad.getHostAddress();
            domain = code;
            } catch (UnknownHostException e) {
               return;
            }
        }
     
        String ipresultStr = HttpClientHelper
                .getHtml(
                        "http://www.youdao.com/smartresult-xml/search.s?type=ip&q="+ip,
                        "GBK", 15 * 1000);
        
        
        Document xmlDoc = DocumentHelper.parseText(ipresultStr);  
        List<Element> list= xmlDoc.selectNodes("//location");
        if(list.size()>0){
            context.put("ipResult", list.get(0).getText());
        }
        context.put("domain", domain);
        if(domain!=null){
            String jsonResult = HttpClientHelper
                    .getHtml("http://whois.www.net.cn/whois/api_whois?host="+domain,"GBK",15);
            JSONObject domainJsonResult=  (JSONObject)    JSONObject.parse(jsonResult);  
            context.put("domainJsonResult", domainJsonResult);
        }
        
        context.put("ip", ip);
        context.put("domain", domain);
        try {
            String ipLeft= ip.substring(0,ip.lastIndexOf("."));
            List<String> ipList = new ArrayList<String> ();
            for(int i=1;i<255;i++){
                ipList.add(ipLeft+i);
            }
            context.put("ipList", ipList);
            
        } catch (Exception e) {
            //DO nothing
        }
        
        

    }
    
    public static void main(String[] args){
        String ipresultStr = HttpClientHelper
                .getHtml(
                        "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=61.172.201.194",
                        "GBK", 15 * 1000);
        System.out.println(ipresultStr);
        System.out.println("===================");

        try {
            Document  xmlDoc = DocumentHelper.parseText(ipresultStr);
            List<Element> list= xmlDoc.selectNodes("//location");
            if(list.size()>0){
                System.out.print(list.get(0).getText());
            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        String jsonResult = HttpClientHelper
                .getHtml("http://whois.www.net.cn/whois/api_whois?host=sina.com.cn","GBK",15*1000);
        JSONObject json=  (JSONObject)    JSONObject.parse(jsonResult);
        System.out.println(json);
        System.out.println(json.get("exp_date"));
        
        System.out.println("192.168.12.122".substring("192.168.12.122".lastIndexOf(".")+1));
    }

}
